package com.weitu.majorHazard.deviceLocation.controller;


import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.majorHazard.deviceLocation.entity.DeviceLocation;
import com.weitu.majorHazard.deviceLocation.service.DeviceLocationService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/5/5.
 */
@Controller
@RequestMapping(value = "majorHazard/deviceLocation")
public class DeviceLocationController extends BaseController {
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"majorHazard/deviceLocation";
    private final DeviceLocationService deviceLocationService;

    @Autowired
    public DeviceLocationController(DeviceLocationService deviceLocationService){
        this.deviceLocationService=deviceLocationService;
    }

    /**
     * 跳到列表页
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 列表页
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.deviceLocationService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);

        List<DeviceLocation> deviceLocationList=this.deviceLocationService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(deviceLocationList);
    }

    /**
     * 跳进添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return MODULE_BATH_PATH+"/add";
    }

    /**
     * 添加
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "装置/部位",methods = "添加装置/部位")
    public AjaxResult addAjax(DeviceLocation deviceLocation){
        deviceLocation.setId(IDGenerator.getId());
        deviceLocation.setDelState(0);
        deviceLocation.setAddUserId(BaseController.getSessionUser().getId());
        deviceLocation.setAddDeptId(BaseController.getSessionUser().getDepartment().getId());
        deviceLocation.setAddTime(DateTimeUtil.formatterDateTime());
        int count=this.deviceLocationService.insertDeviceLocation(deviceLocation);
        if(count==1){
            return AjaxResult.success("保存成功");
        }else {
            return AjaxResult.fail("保存失败");
        }

    }

    /**
     * 跳到编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        DeviceLocation deviceLocation=this.deviceLocationService.queryDeviceLocationById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("deviceLocation",deviceLocation);
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        DeviceLocation deviceLocation=this.deviceLocationService.queryDeviceLocationById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("deviceLocation",deviceLocation);
        modelAndView.setViewName(MODULE_BATH_PATH+"/detail");
        return modelAndView;
    }
    /**
     * 编辑
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(module = "装置/部位",methods = "编辑装置/部位")
    public AjaxResult editAjax(DeviceLocation deviceLocation){
        deviceLocation.setUpdateUserId(BaseController.getSessionUser().getId());
        deviceLocation.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        deviceLocation.setUpdateTime(DateTimeUtil.formatterDateTime());
        int count=this.deviceLocationService.updateDeviceLocation(deviceLocation);
        if(count==1){
            return AjaxResult.success("编辑成功");
        }else{
            return AjaxResult.fail("编辑失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @SystemLog(module = "装置/部位",methods = "删除装置/部位")
    public AjaxResult delete(String id){
        int count=this.deviceLocationService.deleteDeviceLocation(id);
        if(count==1){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.fail("删除失败");
        }
    }

    /**
     * 简单列表
     * @return
     */
    @RequestMapping(value = "/simpleList",method = RequestMethod.GET)
    public String simpleList(){
        return MODULE_BATH_PATH+"/simpleList";
    }

    /**
     * 判断
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/judgement",method = RequestMethod.POST)
    public AjaxResult judgement(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String,Object>();

        putParametersIntoMap(map);
        boolean flag=this.deviceLocationService.judgement(map);
        if(flag)
            return AjaxResult.success("可以添加");
        else
            return AjaxResult.fail("编码重复");

    }

}
